// Copyright 2007-2009, Stanford University. All Rights Reserved.
// Copyright 2010-2011, Dependable Systems Lab, EPFL. All Rights Reserved.
// Copyright 2012, Google Inc. All Rights Reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimers.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimers
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the names of the klee Team, Stanford University, nor the
// names of its contributors may be used to endorse or promote
// products derived from the software without specific prior
// written permission. Neither the names of the Dependable Systems
// Laboratory, EPFL, nor the names of its contributors may be used to
// endorse or promote products derived from the software without prior
// written permission. Neither the names of Google Inc. nor the names of
// its contributors may be used to endorse or promote products derived from
// the software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef SRC_EXPR_EXPRBUILDER_H_
#define SRC_EXPR_EXPRBUILDER_H_

#include "expr/Expr.h"
#include "util/Ref.h"

namespace seven_summits {
namespace expr {

using util::ref;

/// ExprBuilder - Base expression builder class.
class ExprBuilder {
protected:
  ExprBuilder();

public:
  virtual ~ExprBuilder();

  // Expressions

  virtual ref<Expr> Constant(const llvm::APInt &Value) = 0;
  virtual ref<Expr> NotOptimized(const ref<Expr> &Index) = 0;
  virtual ref<Expr> Read(const UpdateList &Updates,
                         const ref<Expr> &Index) = 0;
  virtual ref<Expr> Select(const ref<Expr> &Cond,
                           const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Concat(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Extract(const ref<Expr> &LHS,
                            unsigned Offset, Expr::Width W) = 0;
  virtual ref<Expr> ZExt(const ref<Expr> &LHS, Expr::Width W) = 0;
  virtual ref<Expr> SExt(const ref<Expr> &LHS, Expr::Width W) = 0;
  virtual ref<Expr> Add(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Sub(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Mul(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> UDiv(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> SDiv(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> URem(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> SRem(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Not(const ref<Expr> &LHS) = 0;
  virtual ref<Expr> And(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Or(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Xor(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Shl(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> LShr(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> AShr(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Eq(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Ne(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Ult(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Ule(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Ugt(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Uge(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Slt(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Sle(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Sgt(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;
  virtual ref<Expr> Sge(const ref<Expr> &LHS, const ref<Expr> &RHS) = 0;

  // Utility functions

  ref<Expr> False() { return ConstantExpr::alloc(0, Expr::Bool); }

  ref<Expr> True() { return ConstantExpr::alloc(1, Expr::Bool); }

  ref<Expr> Constant(uint64_t Value, Expr::Width W) {
    return Constant(llvm::APInt(W, Value));
  }
};

/// createDefaultExprBuilder - Create an expression builder which does no
/// folding.
ExprBuilder *createDefaultExprBuilder();

/// createConstantFoldingExprBuilder - Create an expression builder which
/// folds constant expressions.
///
/// Base - The base builder to use when constructing expressions.
ExprBuilder *createConstantFoldingExprBuilder(ExprBuilder *Base);

/// createSimplifyingExprBuilder - Create an expression builder which attemps
/// to fold redundant expressions and normalize expressions for improved
/// caching.
///
/// Base - The base builder to use when constructing expressions.
ExprBuilder *createSimplifyingExprBuilder(ExprBuilder *Base);

} // namespace expr
} // namespace seven_summits

#endif  // SRC_EXPR_EXPRBUILDER_H_
